package com.android.dumiaogou.fragment.member;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.dumiaogou.R;
import com.android.dumiaogou.fragment.BaseLazyFragment;
import com.android.dumiaogou.model.member.MerVoucherDetailData;
import com.android.dumiaogou.model.member.MerVoucherDetailDataCallback;
import com.android.dumiaogou.utils.ConstantsUrl;
import com.android.dumiaogou.utils.HttpUtils;
import com.android.dumiaogou.utils.MyLog;
import com.android.dumiaogou.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/9/27.
 */

public class Fragment_Use_MerVoucher extends BaseLazyFragment {
   @BindView(R.id.voucher_img)
    ImageView voucher_img;
    @BindView(R.id.voucher_title)
    TextView voucher_title;
    @BindView(R.id.voucher_summary)
    TextView voucher_summary;
    @BindView(R.id.start_time)
    TextView start_time;
    @BindView(R.id.end_time)
    TextView end_time;
    @BindView(R.id.voucher_qrcode)
    ImageView voucher_qrcode;
    @BindView(R.id.vertify_code)
    TextView vertify_code;
    @BindView(R.id.user_illustrate)
    TextView user_illustrate;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;


    private boolean networkConnected;
    private static final String ARG_PARAM1 = "param1";

    private String id;


    public static Fragment_Use_MerVoucher newInstance(String id) {
        Fragment_Use_MerVoucher fragment = new Fragment_Use_MerVoucher();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1,id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
           id = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {

        GetDataListData();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_use_mervoucher, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    //获取数据
    private void GetDataListData() {
        String address;
            address = ConstantsUrl.domain + ConstantsUrl.MEMBERMERVOUCHERDETAIL + "?id=" +id;
        MyLog.e("地址", address);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, null, Callback);
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }

    MerVoucherDetailDataCallback Callback = new MerVoucherDetailDataCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
                ObjectUtils.toast(mcontext,e.getMessage());
        }

        @Override
        public void onResponse(MerVoucherDetailData response, int id) {
            if(response.getCode()==0)
            {
           ObjectUtils.photo2(mcontext,response.getData().getThumb(),voucher_img);
                voucher_title.setText(response.getData().getTitle());
                voucher_summary.setText(response.getData().getSummary());
                start_time.setText(response.getData().getStart_time());
                end_time.setText(response.getData().getEnd_time());
                ObjectUtils.photo2(mcontext,response.getData().getQrcode(),voucher_qrcode);
                vertify_code.setText(response.getData().getOrder_id());
                user_illustrate.setText(response.getData().getExplain());
            }
            else {
                    ObjectUtils.toast(mcontext, "网络连接出现异常");

            }
        }
    };

}

